var radioArray = new Array();
var nthDocument = 1;
var docNumCount = 0;
var assortment = [{
	"name": "教育专区",
	"children": [{
		"name": "幼儿教育",
		"children": ["幼儿读物", "少儿英语", "唐诗宋词", "育儿理论经验", "育儿知识"]
	}, {
		"name": "小学教育",
		"children": ["语文", "数学", "英语", "学科竞赛", "小升初"]
	}, {
		"name": "初中教育",
		"children": ["语文", "数学", "英语", "政史地", "物化生"]
	}, {
		"name": "高中教育",
		"children": ["语文作文", "语文阅读", "数学", "英语", "物理", "化学", "生物"]
	}]
}, {
	"name": "专业资料",
	"children": [{
		"name": "网页开发",
		"children": ["前端", "后台", "移动端"]
	}, {
		"name": "经营销售",
		"children": ["经济市场", "金融投资", "财务管理", "金融投资管理", "企业管理", "公共管理"]
	}, {
		"name": "医药卫生",
		"children": ["基础医学", "预防医学", "中医中药", "医学", "临床医学"]
	}, {
		"name": "自然科学",
		"children": ["物理", "化学", "生物", "数学", "天文学", "地理"]
	}, {
		"name": "农林牧渔",
		"children": ["水产养殖", "农学", "林学", "畜牧兽医"]
	}]
}, {
	"name": "使用文档",
	"children": [{
		"name": "PPT模板",
		"children": ["商务", "苹果风", "课程展示", "艺术创意"]
	}, {
		"name": "PPT制作技巧",
		"children": ["图片技巧", "文字技巧", "动画技巧", "音频技巧", "视频技巧"]
	}, {
		"name": "求职",
		"children": ["简历", "面试", "职业规划", "笔试"]
	}, {
		"name": "总结汇报",
		"children": ["学习总结", "工作总结", "实习总结", "其它"]
	}]
}];
//var $oneDoc = $('<div class="style-form doc1">'+
//						'<span class="cancel-upload fa fa-close fa-fw"></span>'+						
//						'<div class="adjust-formin-center">'+
//							'<input type="file" accept="application" />'+
//							'<div class="doc-name_intro">'+
//								'<label class="for-doc-name">文档名:</label>'+
//								'<input type="text" class="upload-doc-name" />'+
//								'<label class="for-introduction">简介:</label>'+
//								'<textarea name="introduction" class="upload-introduction" placeholder="请写下你对本文档的简介"></textarea>'+
//							'</div>'+
//							'<div class="authority-assort">'+
//								'<div class="choose-button">'+
//									'<button class="public-button selected" type="button"><span class="fa fa-check fa-fw"></span>公开</button>'+
//									'<button class="private-button not-selected" type="button">私有</button>'+
//								'</div>'+
//								'<div class="assortment">'+
//									'<button class="assort-button" type="button">选择分类<span class="fa fa-angle-double-down fa-fw"></span></button>'+
//									'<div class="three-select">'+
//										'<select class="first-select" multiple="multiple">'+
//										'</select>'+
//										'<select class="second-select" multiple="multiple">'+
//										'</select>'+
//										'<select class="third-select" multiple="multiple">'+
//										'</select>'+
//									'</div>'+
//								'</div>'+
//							'</div>'+
//						'</div>'+
//					'</div>')
$(function() {
	chooseFileListener();
	chooseButtonListener();
	cancelUploadListener();
	submitListener();
});

function chooseFileListener() {
	//批量编辑disabled
	$(".upload-page .choose-file .edit").prop("disabled", true);
	$(".upload-page .choose-file .edit").addClass("disabled");

	//选择文件按钮click事件
	$(".upload-page .choose-file .choose").click(function() {
		//先为第nthDocument个input type = file 绑定change事件
		$(".upload-page .container .doc" + nthDocument + " input[type='file']").unbind(); //防止多次绑定事件，如果用户多次click，但不change（选择文件），那么就会绑定很多事件在队列里。生成的div就会很多
		$(".upload-page .container .doc" + nthDocument + " input[type='file']").change(function() {
			var docName = $(".upload-page .container .doc" + nthDocument + " input[type='file']").val();
			docName = docName.substring(0, docName.lastIndexOf("."));
			$(".upload-page .container .doc" + nthDocument + " .upload-doc-name").val(docName);
			$(".upload-page .container .doc" + nthDocument).fadeIn();
			$(".upload-page .container .doc" + nthDocument + " .upload-introduction").focus();
			var oneDocument = $(".upload-page .container .doc1").clone(true);
			oneDocument.removeClass("doc1");
			oneDocument.addClass("doc" + (nthDocument + 1));
			oneDocument.css({
				"display": "none"
			})
			$(".upload-page .container .doc" + nthDocument).after(oneDocument);
			
			assortButtonListener(nthDocument);
			radioArray[nthDocument - 1] = "公开";
			nthDocument++;
			docNumCount++;
			if (docNumCount > 1) {
				$(".upload-page .choose-file .edit").prop("disabled", false);
				$(".upload-page .choose-file .edit").removeClass("disabled");
			}
			if (docNumCount >= 1) {
				$(".upload-page .uploadFile").fadeIn();
				$(".upload-page .upload-chos-finish")
					.html('<span class="fa fa-check-circle fa-fw"></span>你已成功选择' + docNumCount + '篇文档<span>请填写完以下信息点击确认上传</span>');
				$(".upload-page .one-document .style-submit").fadeIn();
			}
		});
		$(".upload-page .container .doc" + nthDocument + " input[type='file']").trigger("click");
	});
}

function assortButtonListener(nthDocument) {
	//分类按钮的监听
	$(".doc" + nthDocument + " .assort-button").unbind();
	$(".doc" + nthDocument + " .assort-button").click(function() {
		if ($(this).find("+.three-select").is(":hidden")) {
			$(this).find("+.three-select").show(0);
			isHidden = false;
		} else {
			$(this).find("+.three-select").hide(0);
		}
	});
	//第一列select中的option
	var $firstOption;
	for (var i = 0; i < assortment.length; i++) {
		$firstOption += "<option value='" + assortment[i].name + "'>" + assortment[i].name + "</option>";
	}
	//第二列select中的option
	var $secondOption = secondOptionsPro(0);
	//第三列select中的option
	var $thirdOption = thirdOptionsPro(0, 0);
	//将以上option加到select并设置默认选择
	$(".upload-page .doc" + nthDocument + " .three-select .first-select").empty();
	$(".upload-page .doc" + nthDocument + " .three-select .first-select").append($firstOption);
	$(".upload-page .doc" + nthDocument + " .three-select .second-select").empty();
	$(".upload-page .doc" + nthDocument + " .three-select .second-select").append($secondOption);
	$(".upload-page .doc" + nthDocument + " .three-select .third-select").empty();
	$(".upload-page .doc" + nthDocument + " .three-select .third-select").append($thirdOption);
	$(".upload-page .doc" + nthDocument + " .three-select .first-select option:first-child").prop("selected", true);
	$(".upload-page .doc" + nthDocument + " .three-select .second-select option:first-child").prop("selected", true);
	$(".upload-page .doc" + nthDocument + " .three-select .third-select option:first-child").prop("selected", true);
	changeAssortmentNode(nthDocument);
}

function changeAssortmentNode(nthDocument) {
	var firstIndex = 0; //第一列select选择的下标，默认为第0个
	var secondIndex = 0; //第二列select选择的下标，默认为第0个
	//所有的option click
	$(".upload-page .doc" + nthDocument + " .three-select option").click(function optionClick() {
		var nodeClass = $(this).parent().attr("class");
		if (nodeClass == "first-select") {
			//如果点击的是第一列
			for (var i = 0; i < assortment.length; i++) {
				if (assortment[i].name == $(this).val()) {
					//如果点击的是第一列的option,获得第一列选中的下标
					firstIndex = i;
					break
				}
			}
			//清空第二列的option
			$(".upload-page .doc" + nthDocument + " .three-select .second-select").empty();
			//利用firstIndex，在secondOptionsPro中生产出第二列的option，并append到第二个select中
			$(".upload-page .doc" + nthDocument + " .three-select .second-select").append(secondOptionsPro(firstIndex));
			//设置第二列默认选中第一个option
			$(".upload-page .doc" + nthDocument + " .three-select .second-select option:first-child").prop("selected", true);
			//清空第三列的option
			$(".upload-page .doc" + nthDocument + " .three-select .third-select").empty();
			//产出第三列的option
			$(".upload-page .doc" + nthDocument + " .three-select .third-select").append(thirdOptionsPro(firstIndex, 0));
			//将第三列的option append到第三个select
			$(".upload-page .doc" + nthDocument + " .three-select .third-select option:first-child").prop("selected", true);
		}
		if (nodeClass == "second-select") {
			//如果点击的是第二列
			for (var i = 0; i < assortment[firstIndex].children.length; i++) {
				if (assortment[firstIndex].children[i].name == $(this).val()) {
					//如果点击的是第二列的option,获得第二列选中的下标
					secondIndex = i;
					break;
				}
			}
			//清空第三列
			$(".upload-page .doc" + nthDocument + " .three-select .third-select").empty();
			//利用firstIndex/secondIndex，产出第三列的option
			$(".upload-page .doc" + nthDocument + " .three-select .third-select").append(thirdOptionsPro(firstIndex, secondIndex));
			//将第三列的option append到第三个select
			$(".upload-page .doc" + nthDocument + " .three-select .third-select option:first-child").prop("selected", true);
		}
		//之前的所有列的option解绑事件
		$(".upload-page .doc" + nthDocument + " .three-select option").unbind();
		//为所有的option（因为多了新产生的option）重新绑定以上这个事件
		$(".upload-page .doc" + nthDocument + " .three-select option").click(optionClick);
	});
}



function secondOptionsPro(firstIndex) {
	//产生第二列的option
	var $secondOption;
	for (var i = 0; i < assortment[firstIndex].children.length; i++) {
		$secondOption +=  "<option value='" + assortment[firstIndex].children[i].name + "'>" + assortment[firstIndex].children[i].name + "</option>";
	}
	return $secondOption;
}


function thirdOptionsPro(firstIndex, secondIndex) {
	//产生第三列的option
	var $thirdOption;
	for (var i = 0; i < assortment[firstIndex].children[secondIndex].children.length; i++) {
		$thirdOption +=  "<option value='" + assortment[firstIndex].children[secondIndex].children[i] + "'>" + assortment[firstIndex].children[secondIndex].children[i] + "</option>";
	}
	return $thirdOption;
}


function chooseButtonListener() {
	$(".choose-button button").click(function() {
		if ($(this).attr("class").match("not-selected")) {

			var docIndex = $(this).parent().parent().parent().parent()
				.attr("class").match(/\d/g).join("");

			radioArray[docIndex - 1] = $(this).html();

			updateRadioInput();

			$(this).addClass("selected").removeClass("not-selected");
			$(this).prepend('<span class="fa fa-check fa-fw"></span>');
			$(this).siblings().removeClass("selected").addClass("not-selected");
			$(this).siblings().find(".fa").remove();
		}
	});
}


function cancelUploadListener() {
	$(".upload-page .one-document .cancel-upload").click(function() {
		var $thisParentObj = $(this).parent();
		var index = $thisParentObj.attr("class").match(/\d/g).join("");
		radioArray[index - 1] = 0;
		updateRadioInput();
		$thisParentObj.slideUp();
		docNumCount--;
		if(docNumCount < 2){
			$(".upload-page .choose-file .edit").prop("disabled", true);
			$(".upload-page .choose-file .edit").addClass("disabled");			
		}
		if (docNumCount > 0) {
			$(".upload-page .upload-chos-finish").empty();
			$(".upload-page .upload-chos-finish")
				.html('<span class="fa fa-check-circle fa-fw"></span>你已成功选择' + docNumCount + '篇文档<span>请填写完以下信息点击确认上传</span>');
		} else {
			$(".upload-page .upload-chos-finish").empty();
			$(".upload-page .upload-chos-finish").html('<span class="fa fa-share-alt-square fa-fw"></span>点击选择文件<span>与世界分享你的知识</span>')
			$(".upload-page .one-document .style-submit").fadeOut();
			$(".upload-page .uploadFile").fadeOut();
		}
	});
}

function submitListener() {
	$(".upload-page .uploadFile").click(function() {
		var docs = $(".upload-page .one-document .style-form");
		for (var i = 0; i < docs.length; i++) {
			if (docs.eq(i).is(":hidden")) {
				docs.eq(i).remove();
			}
		}
	});
}


function updateRadioInput() {
	var radioString = "";
	for (var i = 0; i < radioArray.length; i++) {
		if (radioArray[i] != 0) {
			radioString += radioArray[i] + ",";
		}
	}
	radioString = radioString.substring(0, radioString.lastIndexOf(","));
	$(".upload-page .one-document .radioArray").val(radioString);
}
